﻿using BreacherCreature.GameObjects;
using King.Engine.Components;
using King.Engine.Core;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BreacherCreature.Components.Lights
{
    public abstract class LightField : Component
    {
        #region Constants

        protected static float DEFAULT_TRANSPARENCY = .8f;

        #endregion

        #region Fields

        public Flicker _flicker;
        protected FlickerType _flickerType;

        #endregion

        #region Protected Properties

        protected float LightTransparency
        {
            get { return _flickerType == FlickerType.None ? DEFAULT_TRANSPARENCY : (_flicker.FullyOn ? DEFAULT_TRANSPARENCY : _flicker.FlickerTransparency); }
        }

        #endregion


        #region Properties

        public bool TurnedOn = true;
        public Color LightColor;        

        #endregion

        #region Constructors

        public LightField()
        {
            _flickerType = FlickerType.None;
        }
        public LightField(int framesOn, int framesOff, bool startOn)
        {
            _flickerType = FlickerType.Switch;
            _flicker = new Flicker(framesOn, framesOff, startOn);
        }
        public LightField(int framesOn, int framesOff) : this(framesOn, framesOff, true) { }
        public LightField(int framesPerSwitch) : this(framesPerSwitch, framesPerSwitch) { }

        #endregion

        #region Public Methods

        public override void Update(GameTime gameTime)
        {
            if (TurnedOn && _flickerType != FlickerType.None)
                _flicker.Update(gameTime);

            Creature creature = (ActivityManager.CurrentActivity as GameActivity).Creature;
            if (CollidesWith(creature.Bounds))
                creature.OnLightEnter();
        }

        public void FlickerOff()
        {
            _flickerType = FlickerType.Switch;
            _flicker = new FlickerOff();
        }

        public void FlickerOn()
        {
            _flickerType = FlickerType.Switch;
            _flicker = new FlickerOn();
        }

        public abstract bool CollidesWith(Rectangle rect);

        #endregion

        public enum FlickerType
        {
            None,
            Switch,
            Variable
        }
    }
}
